package com.thz.houserental.infrastructure.web.controller;

import com.thz.houserental.infrastructure.advice.response.IgnoreResponseAdvice;
import com.thz.houserental.infrastructure.table.User;
import com.thz.houserental.infrastructure.web.dto.EmailLoginDto;
import com.thz.houserental.infrastructure.web.dto.RegisterDto;
import com.thz.houserental.infrastructure.web.dto.WechatLoginDto;
import com.thz.houserental.infrastructure.web.service.UserService;
import com.thz.houserental.infrastructure.web.vo.CurrentUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/auth")
@Tag(name = "权限控制")
public class AuthController {
    private final UserService userService;
    private final PasswordEncoder passwordEncoder;

    public AuthController(UserService userService, PasswordEncoder passwordEncoder) {
        this.userService = userService;
        this.passwordEncoder = passwordEncoder;
    }

    @Operation(summary = "注册")
    @PostMapping("/register")
    public String showRegistrationForm(@RequestBody @Valid RegisterDto registerDto) {
        return userService.registerNewUserAccount(registerDto);
    }
    
    @PostMapping("/login/email")
    @Operation(summary = "邮箱登录")
    public String loginByEmail(@RequestBody @Valid EmailLoginDto loginDto) {
        return this.userService.loginByEmail(loginDto);
    }
    
    @Operation(summary = "微信登录")
    @PostMapping("/login/wechat")
    public String loginByWechat(@RequestBody @Valid WechatLoginDto loginDto) {
        return this.userService.loginByWechat(loginDto);
    }
    
    @Operation(summary = "测试-获取所有用户账号", description = "密码经过加密的，无法解密，需要账号直接使用注册接口")
    @GetMapping("/getAll")
    public List<User> getAll() {
        return userService.lambdaQuery().list();
    }
    
    @GetMapping("/currentUser")
    @Operation(summary = "获取当前登录用户信息")
    public CurrentUser getCurrentUser() {
        return this.userService.getCurrentUser();
    }
}
